/*******************************************************************************
 * Copyright (C) 2016, Huada Semiconductor Co., Ltd. All rights reserved.
 *
 * This software is owned and published by:
 * Huada Semiconductor Co., Ltd. ("HDSC").
 *
 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
 *
 * This software contains source code for use with HDSC
 * components. This software is licensed by HDSC to be adapted only
 * for use in systems utilizing HDSC components. HDSC shall not be
 * responsible for misuse or illegal use of this software for devices not
 * supported herein. HDSC is providing this software "AS IS" and will
 * not be responsible for issues arising from incorrect user implementation
 * of the software.
 *
 * Disclaimer:
 * HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
 * REGARDING THE SOFTWARE (INCLUDING ANY ACCOMPANYING WRITTEN MATERIALS),
 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
 * WARRANTY OF NONINFRINGEMENT.
 * HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
 * SAVINGS OR PROFITS,
 * EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
 * FROM, THE SOFTWARE.
 *
 * This software may be replicated in part or whole for the licensed use,
 * with the restriction that this Disclaimer and Copyright notice must be
 * included with each copy of this software, whether used in part or whole,
 * at all times.
 */
/******************************************************************************/
/** \file modem.h
 **
 **   - 2019-04-10  1.0  yangjp  First version for modem function.
 **
 ******************************************************************************/
#ifndef __MODEM_H__
#define __MODEM_H__

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "hc32_common.h"

/* C binding of definitions if building with C++ compiler */
#ifdef __cplusplus
extern "C"
{
#endif

/*******************************************************************************
 * Global type definitions ('typedef')
 ******************************************************************************/
/**
 *******************************************************************************
 ** \brief Packet status enumeration definition
 ******************************************************************************/
typedef enum
{
    PACKET_ACK_OK    = 0x00U,
    PACKET_ACK_ERROR = 0x01U,
} en_packet_status_t;

/**
 *******************************************************************************
 ** \brief Packet command enumeration definition
 ******************************************************************************/
typedef enum
{
    PACKET_CMD_HANDSHAKE   = 0x20U,
    PACKET_CMD_APP_UPGRADE = 0x26U,
} en_packet_cmd_t;

/**
 *******************************************************************************
 ** \brief Packet type enumeration definition
 ******************************************************************************/
typedef enum
{
    PACKET_TYPE_CONTROL = 0x11U,
    PACKET_TYPE_DATA    = 0x12U,
} en_packet_type_t;

/*******************************************************************************
 * Global pre-processor symbols/macros ('#define')
 ******************************************************************************/
/* Tick reload value */
#define TICK_RELOAD_VALUE               (0xFFFFFFFFUL)

/* Flash information definition */
#define FLASH_SECTOR_SIZE               (0x2000UL)
#define BOOT_SIZE                       (2U * FLASH_SECTOR_SIZE)
#define APP_UPGRADE_ADDRESS             (FLASH_BASE + BOOT_SIZE - 8U)
#define APP_UPGRADE_FLAG                (0xA5B6C7D8UL)

/* Frame structure definition */
#define FRAME_HEAD                      (0xAC6DU)
#define FRAME_SHELL_SIZE                (0x08U)
#define FRAME_NUM_XOR_BYTE              (0xFFU)

#define FRAME_HEAD_INDEX                (0x00U)
#define FRAME_NUM_INDEX                 (0x02U)
#define FRAME_XORNUM_INDEX              (0x03U)
#define FRAME_LENGTH_INDEX              (0x04U)
#define FRAME_PACKET_INDEX              (0x06U)

/* Packet structure definition */
#define PACKET_INSTRUCT_SIZE            (10U)
#define PACKET_DATA_SIZE                (512U)
#define PACKET_MIN_SIZE                 (PACKET_INSTRUCT_SIZE)
#define PACKET_MAX_SIZE                 (PACKET_DATA_SIZE + PACKET_INSTRUCT_SIZE)

#define PACKET_CMD_INDEX                (FRAME_PACKET_INDEX + 0x00U)
#define PACKET_TYPE_INDEX               (FRAME_PACKET_INDEX + 0x01U)
#define PACKET_RESULT_INDEX             (FRAME_PACKET_INDEX + 0x01U)
#define PACKET_ADDRESS_INDEX            (FRAME_PACKET_INDEX + 0x02U)
#define PACKET_DATA_INDEX               (FRAME_PACKET_INDEX + PACKET_INSTRUCT_SIZE)

/* USART information definition */
#define USART_UNIT                      (M4_USART3)
#define USART_UNIT_CLOCK                (PWC_FCG1_PERIPH_USART3)
#define USART_BAUDRATE                  (921600UL)

#define USART_RX_PORT                   (PortE)
#define USART_RX_PIN                    (Pin04)
#define USART_RX_FUNC                   (Func_Usart3_Rx)

#define USART_TX_PORT                   (PortE)
#define USART_TX_PIN                    (Pin05)
#define USART_TX_FUNC                   (Func_Usart3_Tx)

/* Frame communication timeout(ms) definition */
#define FRAME_RECV_TIMEOUT              (5UL)

/*******************************************************************************
 * Global variable definitions ('extern')
 ******************************************************************************/

/*******************************************************************************
  Global function prototypes (definition in C source)
 ******************************************************************************/
en_result_t Modem_Process(uint32_t u32Timeout);

#ifdef __cplusplus
}
#endif

#endif /* __MODEM_H__ */

/*******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
